package com.zxc.gulimall.cart.conrtoller;

import com.zxc.common.constant.AuthServerConstant;
import com.zxc.common.utils.R;
import com.zxc.gulimall.cart.feign.ProductFeignService;
import com.zxc.gulimall.cart.interceptor.CartInterceptor;
import com.zxc.gulimall.cart.service.CartService;
import com.zxc.gulimall.cart.vo.Cart;
import com.zxc.gulimall.cart.vo.CartItem;
import com.zxc.gulimall.cart.vo.UserInfoTo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.concurrent.ExecutionException;

@Controller
public class CartController {

    @Autowired
    private CartService cartService;

    @GetMapping(value = "/currentUserCartItem")
    @ResponseBody
    public List<CartItem> getCurrentUserCartItem(){
        return cartService.getUserCartItems();
    }

    @GetMapping(value = "/deleteItem")
    public String deleteItem(@RequestParam(value = "skuId")Long skuId){
        cartService.deleteItem(skuId);
        return "redirect:http://cart.gulimall.com/cart.html";
    }

    @GetMapping(value = "/changeItemCount")
    public String changeItemCount(@RequestParam(value = "skuId")Long skuId,@RequestParam(value = "num")Integer num){
        cartService.changeItemCount(skuId,num);
        return "redirect:http://cart.gulimall.com/cart.html";
    }

    @GetMapping(value = "/checkItem")
    public String checkItem(@RequestParam(value = "skuId")Long skuId,@RequestParam(value = "check")Integer check){
        cartService.checkItem(skuId,check);
        return "redirect:http://cart.gulimall.com/cart.html";
    }

    @GetMapping(value = "cart.html")
    public String cartListPage(Model model) throws ExecutionException, InterruptedException {
        Cart cart = cartService.getCart();
        model.addAttribute("cart",cart);
        return "cartList";
    }

    //添加商品到购物车
    @GetMapping(value = "/addToCart")
    public String addToCart(@RequestParam(value = "skuId")Long skuId, @RequestParam(value = "num")Integer num, RedirectAttributes redirectAttributes) throws ExecutionException, InterruptedException {
        //System.out.println("商品的Id为"+skuId+"==="+"数量为"+num);
        cartService.addToCart(skuId,num);
        redirectAttributes.addAttribute("skuId",skuId);
        return "redirect:http://cart.gulimall.com/addToCartSuccess.html";
    }

    @GetMapping(value = "/addToCartSuccess.html")
    public String addToCartSuccessPage(@RequestParam("skuId") Long skuId,Model model){
        //重定向此页面，再次查询
        CartItem cartItem = cartService.getCartItem(skuId);
        model.addAttribute("item",cartItem);
        return "success";
    }

}

